package com.jyu.hakkatranslation.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.jyu.hakkatranslation.pojo.WordEntry;
import com.jyu.hakkatranslation.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.jyu.hakkatranslation.vo.EntryVo;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@Slf4j
public class SearchController {
    @Autowired
    SearchService searchService;
    @GetMapping("/search")
    public String search(@RequestParam("item") String item,
                         @RequestParam("place") String place,
                         @RequestParam("word") String word, Model model, HttpSession httpSession){
        EntryVo entryVo = new EntryVo();
        entryVo.setItem(item);
        entryVo.setPlace(place);
        entryVo.setWord(word);
        List<WordEntry> wordEntries = searchService.searchWordEntry(entryVo);
        /*判断是否为空或null*/
        httpSession.setAttribute("hasWordEntries",true);
        if (CollectionUtil.isEmpty(wordEntries)){
            httpSession.setAttribute("hasWordEntries",false);
        }
        log.info("查询结果集===> {}", wordEntries);
        model.addAttribute("wordEntries",wordEntries);
        return "Result";
    }

//    @ResponseBody
//    @PostMapping("/search")
//    public String Search(String ss){
//        WordEntry woed = searchService.search(ss);
//        System.out.println(woed);
//        return "woed";
//    }
}
